﻿using System;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;
using Monkey.Warrenty.Enum;

namespace MonkeyWarranty.Web.EmployeePages.InstantTickets
{
    public partial class EditService : System.Web.UI.Page
    {
        #region Properties

        private InstantTicket_Service _ticketService;
        private InstantTicket_Service TicketService
        {
            get
            {
                if (_ticketService == null)
                {
                    if (TicketServiceId > 0)
                    {
                        _ticketService = InstantTicketManager.GetTicketServiceById(TicketServiceId);
                    }
                }
                return _ticketService;
            }
        }

        private static CustomerBiz _customerBiz;
        private static CustomerBiz CustomerManager
        {
            get { return _customerBiz ?? (_customerBiz = new CustomerBiz()); }
        }

        protected long TicketId
        {
            get { return (TicketService != null) ? TicketService.InstantTicketId : 0; }
        }

        private InstantTicketServiceView _serviceView;

        private InstantTicketServiceView CurrentServiceView
        {
            get { return _serviceView ?? (_serviceView = ServiceManager.GetServiceViewById(TicketServiceId)); }
        }

        private long _ticketServiceId;
        private long TicketServiceId
        {
            get
            {
                if (_ticketServiceId <= 0) long.TryParse(Request.Params["id"], out _ticketServiceId);
                return _ticketServiceId;
            }
        }

        private static ServiceBiz _serviceBiz;
        private static ServiceBiz ServiceManager
        {
            get { return _serviceBiz ?? (_serviceBiz = new ServiceBiz()); }
        }

        private static InstantTicketBiz _instantTicketBiz;
        private static InstantTicketBiz InstantTicketManager
        {
            get { return _instantTicketBiz ?? (_instantTicketBiz = new InstantTicketBiz()); }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (TicketServiceId <= 0 || TicketService == null)
                {
                    errorMessage.Visible = true;
                    lblErrorMessage.Text = Resources.GlobalStrings.ServiceNotExisted;
                    btnProcessing.Enabled = false;
                    btnComplete.Enabled = false;
                    return;
                }

                LoadData();
            }
        }

        protected void btnProcessing_Click(object sender, EventArgs e)
        {
            errorMessage.Visible = false;
            lblErrorMessage.Text = string.Empty;

            // Call Biz Save Service
            InstantTicketManager.UpdateTicketServiceStatus(TicketServiceId, (int)InstantServiceStatus.Processing, txtDescription.Text, txtNote.Text);

            errorMessage.Visible = true;
            lblErrorMessage.Text = Resources.GlobalStrings.UpdateSuccess;

            btnProcessing.Visible = false;
        }

        protected void btnComplete_Click(object sender, EventArgs e)
        {
            errorMessage.Visible = false;
            lblErrorMessage.Text = string.Empty;

            // Call Biz Save Service
            InstantTicketManager.UpdateTicketServiceStatus(TicketServiceId, (int)InstantServiceStatus.Completed, txtDescription.Text, txtNote.Text);

            errorMessage.Visible = true;
            lblErrorMessage.Text = Resources.GlobalStrings.UpdateSuccess;

            btnProcessing.Visible = false;
            btnComplete.Visible = false;
        }

        #endregion

        #region Helpers

        private void LoadData()
        {
            if (CurrentServiceView == null)
            {
                return;
            }

            // instant ticket
            var ticket = InstantTicketManager.GetById(TicketId);
            if (ticket == null)
            {
                return;
            }
            ltrTicketCode.Text = ticket.Code;
            ltrContent.Text = ticket.TicketContent;
            ltrDistance.Text = string.Format("{0:0.00}", ticket.Distance);
            ltrReceivedDate.Text = string.Format("{0:dd/MM/yyyy HH:mm}", ticket.CreatedDate);
            ltrDescription.Text = string.IsNullOrEmpty(ticket.Description) ? string.Empty : ticket.Description.Replace("\n", "<br />");

            // customer
            var customer = CustomerManager.GetById(ticket.CustomerId);
            if (customer != null)
            {
                ltrCustomerName.Text = customer.Name;
                ltrCustomerAddress.Text = customer.Address;
                ltrCustomerPhone.Text = customer.Phone;
                ltrCustomerEmail.Text = customer.Email;
            }

            // details
            ltrServiceName.Text = CurrentServiceView.ServiceName;
            ltrQuantity.Text = CurrentServiceView.Quantity.ToString();
            txtDescription.Text = CurrentServiceView.Description;
            txtNote.Text = CurrentServiceView.Note;
            ltrStartDate.Text = CurrentServiceView.StartDateString;
            ltrEndDate.Text = CurrentServiceView.EndDateString;
            ltrServiceType.Text = CurrentServiceView.ServiceTypeName;
            ltrError.Text = CurrentServiceView.ErrorName;
            ltrEmployee.Text = CurrentServiceView.EmployeeName;
            ltrStatus.Text = CurrentServiceView.InstantServiceStatusName;

            if ((InstantServiceStatus)CurrentServiceView.InstantServiceStateId != InstantServiceStatus.Received)
            {
                btnProcessing.Visible = false;
            }
        }

        #endregion
    }
}